<template>
  <view class="content">
    <!-- 搜索 -->
    <!-- @click可以给原生的dom对象注册事件 MySearch不是原生的dom对象 是组件 -->
    <!-- @子组件派发的事件名="自己自定义的事件名" -->
    <!-- .native修饰符 可以给组件的根元素注册事件 -->
    <view class="search-box">
      <MySearch @click.native="goSearch"></MySearch>
    </view>
    <!-- 轮播图区域 -->
    <swiper class="swiper" indicator-dots autoplay circular :interval="2000">
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <!-- 声明式 navigator 编程式 注册事件 @click -->
        <navigator
          :url="`/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`"
          class="swiper-item"
        >
          <image :src="item.image_src" />
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view
        class="nav-item"
        @click="goNavItem(item)"
        v-for="(item, index) in navList"
        :key="index"
      >
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧 1 个大图 -->
          <view
            class="left-img-box"
            @click="goToProductList(item.product_list[0])"
          >
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
            ></image>
          </view>

          <!-- 右侧 4 个小图 -->
          <view class="right-img-box">
            <block v-for="(product, idx) in item.product_list" :key="idx">
              <view
                class="right-img-item"
                v-if="idx !== 0"
                @click="goToProductList(product)"
              >
                <image
                  :src="product.image_src"
                  :style="{ width: product.image_width + 'rpx' }"
                  mode="widthFix"
                ></image>
              </view>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import MySearch from '@/components/my-search'
import badge from '@/mixins/badge.js' // 代码混入
export default {
  mixins: [badge],
  components: {
    MySearch
  },
  data() {
    return {
      swiperList: [], // 轮播图列表
      navList: [], // 分类列表
      floorList: [] // 楼层列表
    }
  },
  created() {
    this.getSwiperList()
    this.getNavList()
    this.getFloorList()
  },
  methods: {
    // 点击搜索框事件
    goSearch() {
      uni.navigateTo({
        url: '/pages_sub1/search/search'
      })
    },
    // 跳转到商品列表
    goToProductList(product) {
      // split 将字符串以?拆分成数组
      const query = product.navigator_url.split('?')[1]
      uni.navigateTo({
        url: '/pages_sub1/product-list/product-list?' + query
      })
    },
    // 获取楼层列表
    async getFloorList() {
      const {
        message,
        meta: { status }
      } = await this.$http.get('/home/floordata')
      if (status !== 200) return this.$showMsg()
      this.floorList = message
    },
    // 获取分类列表
    async getNavList() {
      const {
        message,
        meta: { status }
      } = await this.$http.get('/home/catitems')
      if (status !== 200) return this.$showMsg()
      this.navList = message
    },
    // 获取轮播图列表
    async getSwiperList() {
      const {
        message,
        meta: { status }
      } = await this.$http.get('/home/swiperdata')
      if (status !== 200) return this.$showMsg()
      // 请求成功
      this.swiperList = message
    },
    // 跳转到其他页面
    goNavItem(item) {
      console.log(item)
      if (item.name === '分类')
        uni.switchTab({
          url: '/pages/category/category'
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  .search-box {
    // 设置吸顶效果
    position: sticky;
    left: 0;
    top: 0;
    z-index: 999;
  }
  .swiper {
    height: 400rpx;
    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }
  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 30rpx 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }
  .floor-title {
    display: flex;
    width: 100%;
    height: 60rpx;
  }
  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .floor-img-box {
    display: flex;
    padding-left: 10rpx;
  }
}
</style>
